package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Keyword;
import com.mall.db.service.KeywordService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 关键字表 前端控制器
 * </p>
 *
 * @author zzz
 * @since 2021-09-24
 */
@Controller
@RequestMapping("/keyword/kc")
public class KeywordController extends BaseController<Keyword>{

    @Resource(name = "keywordService")
    private KeywordService ks;

    @Override
    public Map<String,Object> find(Keyword keyword){


        PageHelper.startPage(keyword.getPage(),keyword.getLimit());

        QueryWrapper<Keyword> qw = new QueryWrapper<>();

        List<Keyword> list = ks.list(qw);

        PageInfo<Keyword> page = new PageInfo<>(list,keyword.getLimit());

        return this.findMap(page.getList(), page.getTotal());
    }


    @Override
    public Map<String, Object> add(Keyword keyword) {
        boolean k = ks.save(keyword);
        return this.iud("新增",k,keyword);
    }

    @Override
    public Map<String, Object> update(Keyword keyword) {
        boolean b = ks.updateById(keyword);
        return this.iud("编辑", b, keyword);
    }

    @Override
    public Map<String, Object> delete(Keyword keyword) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(keyword.getId())) {
            ks.removeById(keyword.getId());
            map.put("is", true);
            map.put("msg", "删除成功!");
        } else {
            map.put("is", false);
            map.put("msg", "删除失败!");
        }
        return map;
    }

}

